package com.tlb.ejb;

import com.tlb.dto.Poster;
import com.tlb.dto.Moniker;
import java.util.*;
import javax.persistence.*;

public class MonikerDAO {

    private static com.tlb.web.Util util = com.tlb.web.Util.getUtil();
    
    public MonikerDAO() {
    }

    public static void main(String args[]) {


    }

    public Moniker getMoniker(int monikerId) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();

            Moniker m = (Moniker) (em.createQuery(
                    "select m from Moniker m where monikerId = :id").setParameter("id", monikerId).getSingleResult());

            tx.commit();
            em.close();

            if (m != null && m.getMonikerId() > 0) {
                m.setName(fixHTML(m.getName()));
                m.setTagLine(fixHTML(m.getTagLine()));
                return m;
            }

        } catch (Exception e) {
            System.err.println("Error getting moniker(id): " + monikerId);
            e.printStackTrace();
        }

        return null;
    }

    public Moniker getMoniker(String name) {
        try {

            if (name == null || name.trim().equals("")) {
                return null;
            }

            name = name.trim();
            name = name.replace("^\\s+", "");

            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            Moniker m = null;
            try {
                m = (Moniker) (em.createQuery(
                        "select m from Moniker m where name = :name").setParameter("name", name).getSingleResult());
            } catch (NoResultException ex) {
                System.out.println("MonikerDAO.getMoniker(String) not found: " + name);
            }
            tx.commit();
            em.close();

            if (m != null && m.getMonikerId() > 0) {
                return m;
            }

        } catch (Exception e) {
            System.err.println("Error getting moniker(creds): " + name);
            e.printStackTrace();
        }

        return null;
    }

    public String fixHTML(String str) throws Exception {
        if (str == null) {
            //throw new Exception("Null passed to MonikerDAO.fixHTML");
        	System.out.println("Shithead was here.");
        	str = "Bad data...";
        }
        str = str.replaceAll("<", "&lt; ");
        str = str.replaceAll(">", "&gt; ");
        return str;
    }

    public String fixHTML(String str, int maxLen) throws Exception {
        if (str == null) {
            throw new Exception("Null passed to MonikerDAO.fixHTML");
        }
        if (str.length() > maxLen) {
            str = str.substring(0, maxLen - 3) + "...";
        }
        str = str.replaceAll("<", "&lt; ");
        str = str.replaceAll(">", "&gt; ");
        return str;
    }

    public void saveMoniker(Moniker m, boolean newMon) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();


            if (m.getName() != null && m.getName().length() > 45) {
                m.setName(m.getName().substring(0, 44));
            }

            if (m.getTagLine() != null && m.getTagLine().length() > 45) {
                m.setTagLine(m.getTagLine().substring(0, 44));
            }

            if (newMon) {
                em.persist(m);
            } else {
                em.merge(m);
            }

            tx.commit();
            em.close();


        } catch (Exception e) {
            System.err.println("Error saving Moniker: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void saveMoniker(Moniker m, int posterId) {
        try {
            EntityManager em = util.getPersistenceFactory().createEntityManager();
            EntityTransaction tx = em.getTransaction();
            tx.begin();


            if (m.getName() != null && m.getName().length() > 45) {
                m.setName(m.getName().substring(0, 44));
            }

            if (m.getTagLine() != null && m.getTagLine().length() > 45) {
                m.setTagLine(m.getTagLine().substring(0, 44));
            }

            Poster p = new PosterDAO().getPoster(posterId);
            m.setPoster(p);

            em.merge(m);

            tx.commit();
            em.close();


        } catch (Exception e) {
            System.err.println("Error saving Moniker: " + e.getMessage());
            e.printStackTrace();
        }

    }
}

